home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-07-03 | 19.2 KB | 591 lines |
-
- package borland.samples.jbcl.treecontrol;
-
- import java.awt.*;
- import java.awt.event.*;
- import borland.jbcl.control.*;
- import borland.jbcl.layout.*;
- import borland.jbcl.model.*;
- import borland.jbcl.view.*;
-
- /**
- * A simple sample application that shows how to use the tree control.
- *
- * @version 1.0, 06/10/97
- * @author Borland Development Team
- */
- public class Frame1 extends DecoratedFrame {
- BorderLayout borderLayout1 = new BorderLayout();
- MenuBar menuBar1 = new MenuBar();
- Menu menuFile = new Menu();
- MenuItem menuFileExit = new MenuItem();
- Menu menuHelp = new Menu();
- MenuItem menuHelpAbout = new MenuItem();
- SplitPanel splitPanel1 = new SplitPanel();
- BevelPanel bevelPanel1 = new BevelPanel();
- BevelPanel bevelPanel2 = new BevelPanel();
- AttributePanel attributePanel1 = new AttributePanel();
- TreeControl treeControl1 = new TreeControl();
- Menu menuControl = new Menu();
- CheckboxMenuItem menuControlReadOnly = new CheckboxMenuItem();
- MenuItem menuControlInsertSibling = new MenuItem();
- MenuItem menuControlInsertChild = new MenuItem();
- MenuItem menuControlRemoveItem = new MenuItem();
- CheckboxMenuItem menuControlShowGlyphs = new CheckboxMenuItem();
- Menu menu1 = new Menu();
- MenuItem menuControlPluses = new MenuItem();
- MenuItem menuConrolArrows = new MenuItem();
- BorderLayout borderLayout2 = new BorderLayout();
- BorderLayout borderLayout3 = new BorderLayout();
- Menu menu2 = new Menu();
- MenuItem menuControlTextEditor = new MenuItem();
- MenuItem menuControlChoiceEditor = new MenuItem();
-
- /**
- * Construct the frame and it's UI elements
- */
- public Frame1() {
- try {
- jbInit();
- fillTree();
- setupAttributes();
- }
- catch (Exception e) {
- borland.jbcl.util.Diagnostic.printStackTrace(e);
- }
- }
-
- /**
- * Contains the UI elements and UI properties of the application's main frame
- */
- public void jbInit() throws Exception{
- this.setLayout(borderLayout1);
- this.setTitle("Simple TreeControl Sample");
- this.setFont( new Font("Dialog", 0, 11));
- menuFile.setLabel("File");
- menuFileExit.setLabel("Exit");
- menuFileExit.addActionListener(new Frame1_menuFileExit_ActionAdapter(this));
- menuHelp.setLabel("Help");
- menuHelpAbout.setLabel("About");
- menuControl.setLabel("Control");
- menuControlReadOnly.setLabel("Read Only");
- menuControlInsertSibling.setLabel("Insert Sibling");
- menuControlInsertSibling.addActionListener(new Frame1_menuControlInsertSibling_actionAdapter(this));
- menuControlInsertChild.setLabel("Insert Child");
- menuControlInsertChild.addActionListener(new Frame1_menuControlInsertChild_actionAdapter(this));
- menuControlRemoveItem.setLabel("Remove Item");
- menuControlShowGlyphs.setLabel("Show Glyphs");
- menu1.setLabel("Style");
- menuControlPluses.setLabel("Pluses");
- menuControlPluses.addActionListener(new Frame1_menuControlPluses_actionAdapter(this));
- menuConrolArrows.setLabel("Arrows");
- menu2.setLabel("Editor");
- menuControlTextEditor.setLabel("Text Item Editor");
- menuControlTextEditor.addActionListener(new Frame1_menuControlTextEditor_actionAdapter(this));
- menuControlChoiceEditor.setLabel("Choice Item Editor");
- menuControlChoiceEditor.addActionListener(new Frame1_menuControlChoiceEditor_actionAdapter(this));
- menuConrolArrows.addActionListener(new Frame1_menuConrolArrows_actionAdapter(this));
- menuControlShowGlyphs.addActionListener(new Frame1_menuControlShowGlyphs_actionAdapter(this));
- menuControlRemoveItem.addActionListener(new Frame1_menuControlRemoveItem_actionAdapter(this));
- menuControlReadOnly.addItemListener(new Frame1_menuControlReadOnly_itemAdapter(this));
- bevelPanel2.setLayout(borderLayout2);
- bevelPanel1.setLayout(borderLayout3);
- menuHelpAbout.addActionListener(new Frame1_menuHelpAbout_ActionAdapter(this));
- menuFile.add(menuFileExit);
- menuHelp.add(menuHelpAbout);
- menuBar1.add(menuFile);
- menuBar1.add(menuControl);
- menuBar1.add(menuHelp);
- //treeControl1.setEditInPlace(false);
- treeControl1.setExpandByDefault(true);
- treeControl1.addSelectionListener(new Frame1_treeControl1_selectionAdapter(this));
- treeControl1.addMouseListener(new Frame1_treeControl1_mouseAdapter(this));
- treeControl1.addComponentListener(new Frame1_treeControl1_componentAdapter(this));
- this.setMenuBar(menuBar1);
- this.add(splitPanel1, BorderLayout.CENTER);
- splitPanel1.add(bevelPanel1, new PaneConstraints("bevelPanel1", "bevelPanel1", PaneConstraints.LEFT, 0.3f));
- bevelPanel1.add(treeControl1, BorderLayout.CENTER);
- splitPanel1.add(bevelPanel2, new PaneConstraints("bevelPanel1", "bevelPanel1", PaneConstraints.RIGHT, 0.7f));
- bevelPanel2.add(attributePanel1, BorderLayout.CENTER);
- menuControl.add(menuControlInsertSibling);
- menuControl.add(menuControlInsertChild);
- menuControl.add(menuControlRemoveItem);
- menuControl.addSeparator();
- menuControl.add(menuControlReadOnly);
- menuControl.add(menuControlShowGlyphs);
- menuControl.addSeparator();
- menuControl.add(menu1);
- menuControl.add(menu2);
- menu1.add(menuControlPluses);
- menu1.add(menuConrolArrows);
- menu2.add(menuControlTextEditor);
- menu2.add(menuControlChoiceEditor);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- /**
- * Setup the inital attribute list and default values in
- * the attribute panel by calling the InsertSibling() method on
- * the attributePanel.
- */
- public void setupAttributes() {
- attributePanel1.addItem("Size", "");
- attributePanel1.addItem("Painter", treeControl1.getViewManager().getPainter(treeControl1.getRoot(), new Object(), 0).toString());
- attributePanel1.addItem("Editor", treeControl1.getViewManager().getEditor(treeControl1.getRoot(), new Object(), 0).toString());
- attributePanel1.addItem("Style", "");
- attributePanel1.addItem("Selected Item", "");
-
- if (treeControl1.getStyle()==borland.jbcl.view.TreeView.STYLE_PLUSES)
- attributePanel1.setValue("Style", "Pluses");
- else if (treeControl1.getStyle()==borland.jbcl.view.TreeView.STYLE_ARROWS)
- attributePanel1.setValue("Style", "Arrows");
- }
-
- /**
- * Setup the inital tree structure using addChild(). Later we'll show
- * how to build on this using the model/view architecture.
- */
- private void fillTree() {
- // Add the root
- GraphLocation root = treeControl1.setRoot("Root Node");
-
- // Add the first child to the root
- GraphLocation first = treeControl1.addChild(root, "First Child");
-
- // Add children to the first child of the root
- treeControl1.addChild(first, "First child item 1");
- treeControl1.addChild(first, "First child item 2");
- treeControl1.addChild(first, "First child item 3");
-
- // Add the first child to the root
- GraphLocation second = treeControl1.addChild(root, "Second Child");
-
- // Add children to the fist child of the root
- treeControl1.addChild(second, "Second child item 1");
- treeControl1.addChild(second, "Second child item 2");
- treeControl1.addChild(second, "Second child item 3");
-
- // Select the first items
- treeControl1.setSubfocus(treeControl1.getRoot());
- }
-
- /**
- * Use the itemStateChanged event to get notification when the
- * state of a CheckboxMenuItem has changed. This is used here
- * to toggle the the EditInPlace property and the ReadOnly
- * property of the tree.
- */
- void menuControlReadOnly_itemStateChanged(ItemEvent e) {
- treeControl1.setEditInPlace(!(menuControlReadOnly.getState()));
- treeControl1.setReadOnly(!(menuControlReadOnly.getState()));
- }
-
- /**
- * Use the componentResized event to get notification when
- * the tree's size has changed. This is used here to update
- * the attribute panel with the trees new size.
- */
- void treeControl1_componentResized(ComponentEvent e) {
- attributePanel1.setValue("Size", treeControl1.getSize().toString());
- }
-
- /**
- * Use the mouseClicked event to get notification when a
- * mouse click has been performed on the control. This is used
- * here to expand and collapse expandable/collapsable nodes of the
- * TreeControl.
- */
- void treeControl1_mouseClicked(MouseEvent e) {
- GraphLocation[] selections = treeControl1.getSelection().getAll();
- if (e.getClickCount() == 2) {
- if (treeControl1.isExpanded(selections[0]))
- treeControl1.collapse(selections[0]);
- else
- treeControl1.expand(selections[0]);
- }
- }
-
- /**
- * Use the selectionChanged event to get notification when the
- * selection has changed in the control. This is used here to update
- * the attribute panel with the current selection in the control.
- * Using this event is the preferred way to determine the selected
- * item in any JBCL control.
- */
- void treeControl1_selectionChanged(GraphSelectionEvent e) {
- GraphLocation[] selections = treeControl1.getSelection().getAll();
- attributePanel1.setValue("Selected Item", (String) treeControl1.get(selections[0]));
- }
-
- /**
- * Insert an item as a sibling to the selected node
- */
- void menuControlInsertSibling_actionPerformed(ActionEvent e) {
- GraphLocation[] selections = treeControl1.getSelection().getAll();
- treeControl1.addChild(selections[0].getParent(), "New sibling item");
- }
-
- /**
- * Insert an item as a child to the selected node
- */
- void menuControlInsertChild_actionPerformed(ActionEvent e) {
- GraphLocation[] selections = treeControl1.getSelection().getAll();
- treeControl1.addChild(selections[0], "New child item");
- }
-
- /**
- * Remove the selected item and it's children
- */
- void menuControlRemoveItem_actionPerformed(ActionEvent e) {
- GraphLocation[] selections = treeControl1.getSelection().getAll();
- treeControl1.remove(selections[0]);
- }
-
- /**
- * Set the Style property on the tree control to STYLE_PLUSES
- */
- void menuControlPluses_actionPerformed(ActionEvent e) {
- treeControl1.setStyle(borland.jbcl.view.TreeView.STYLE_PLUSES);
- attributePanel1.setValue("Style", "Pluses");
- }
-
- /**
- * Set the Style property on the tree control to STYLE_ARROWS
- */
- void menuConrolArrows_actionPerformed(ActionEvent e) {
- treeControl1.setStyle(borland.jbcl.view.TreeView.STYLE_ARROWS);
- attributePanel1.setValue("Style", "Arrows");
- }
-
- /**
- * Set the tree control's editor to a TextItemEditor()
- */
- void menuControlTextEditor_actionPerformed(ActionEvent e) {
- treeControl1.setViewManager(new BasicViewManager(new FocusableItemPainter(new SelectableTextItemPainter()),new TextItemEditor()));
- }
-
- /**
- * Set the tree control's editor to a ChoiceItemEditor()
- */
- void menuControlChoiceEditor_actionPerformed(ActionEvent e) {
- String[] choices = new String[] {"Selected Item 1", "Selected Item 2", "Selected Item 3"};
- treeControl1.setViewManager(new BasicViewManager(new FocusableItemPainter(new SelectableTextItemPainter()), new ChoiceItemEditor(choices)));
- }
-
- void menuControlShowGlyphs_actionPerformed(ActionEvent e) {
-
- }
-
- /**
- * Exit the sample application by calling System.exit(int).
- * This returns an exit code specified by int to the
- * calling process.
- */
- public void menuFileExit_actionPerformed(ActionEvent e) {
- System.exit(0);
- }
-
- /**
- * Display an about box for the sample application. This uses
- * the Frame1_AboutBox class also contained in this source
- * file.
- */
- public void menuHelpAbout_actionPerformed(ActionEvent e) {
- Frame1_AboutBox dlg = new Frame1_AboutBox(this);
- Dimension dlgSize = dlg.getPreferredSize();
- Dimension frmSize = getSize();
- Point loc = getLocation();
- dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
- dlg.setModal(true);
- dlg.show();
- }
- }
-
- class Frame1_menuFileExit_ActionAdapter implements ActionListener {
- Frame1 adaptee;
-
- Frame1_menuFileExit_ActionAdapter(Frame1 adaptee) {
- this.adaptee = adaptee;
- }
-
- public void actionPerformed(ActionEvent e) {
- adaptee.menuFileExit_actionPerformed(e);
- }
- }
-
- class Frame1_menuHelpAbout_ActionAdapter implements ActionListener {
- Frame1 adaptee;
-
- Frame1_menuHelpAbout_ActionAdapter(Frame1 adaptee) {
- this.adaptee = adaptee;
- }
-
- public void actionPerformed(ActionEvent e) {
- adaptee.menuHelpAbout_actionPerformed(e);
- }
- }
-
- class Frame1_InsetsPanel extends Panel {
- protected Insets insets;
-
- public Insets getInsets() {
- return insets == null ? super.getInsets() : insets;
- }
-
- public void setInsets(Insets insets) {
- this.insets = insets;
- }
- }
-
- class Frame1_treeControl1_componentAdapter extends java.awt.event.ComponentAdapter {
- Frame1 adaptee;
-
- Frame1_treeControl1_componentAdapter(Frame1 adaptee) {
- this.adaptee = adaptee;
- }
-
- public void componentResized(ComponentEvent e) {
- adaptee.treeControl1_componentResized(e);
- }
- }
-
- class Frame1_treeControl1_mouseAdapter extends java.awt.event.MouseAdapter {
- Frame1 adaptee;
-
- Frame1_treeControl1_mouseAdapter(Frame1 adaptee) {
- this.adaptee = adaptee;
- }
-
- public void mouseClicked(MouseEvent e) {
- adaptee.treeControl1_mouseClicked(e);
- }
- }
-
- class Frame1_treeControl1_selectionAdapter extends borland.jbcl.model.GraphSelectionAdapter {
- Frame1 adaptee;
-
- Frame1_treeControl1_selectionAdapter(Frame1 adaptee) {
- this.adaptee = adaptee;
- }
-
- public void selectionChanged(GraphSelectionEvent e) {
- adaptee.treeControl1_selectionChanged(e);
- }
- }
-
- class Frame1_menuControlReadOnly_itemAdapter implements java.awt.event.ItemListener {
- Frame1 adaptee;
-
- Frame1_menuControlReadOnly_itemAdapter(Frame1 adaptee) {
- this.adaptee = adaptee;
- }
-
- public void itemStateChanged(ItemEvent e) {
- adaptee.menuControlReadOnly_itemStateChanged(e);
- }
- }
-
- class Frame1_menuControlInsertSibling_actionAdapter implements java.awt.event.ActionListener {
- Frame1 adaptee;
-
- Frame1_menuControlInsertSibling_actionAdapter(Frame1 adaptee) {
- this.adaptee = adaptee;
- }
-
- public void actionPerformed(ActionEvent e) {
- adaptee.menuControlInsertSibling_actionPerformed(e);
- }
- }
-
- class Frame1_menuControlInsertChild_actionAdapter implements java.awt.event.ActionListener {
- Frame1 adaptee;
-
- Frame1_menuControlInsertChild_actionAdapter(Frame1 adaptee) {
- this.adaptee = adaptee;
- }
-
- public void actionPerformed(ActionEvent e) {
- adaptee.menuControlInsertChild_actionPerformed(e);
- }
- }
-
- class Frame1_menuControlRemoveItem_actionAdapter implements java.awt.event.ActionListener {
- Frame1 adaptee;
-
- Frame1_menuControlRemoveItem_actionAdapter(Frame1 adaptee) {
- this.adaptee = adaptee;
- }
-
- public void actionPerformed(ActionEvent e) {
- adaptee.menuControlRemoveItem_actionPerformed(e);
- }
- }
-
- class Frame1_menuControlShowGlyphs_actionAdapter implements java.awt.event.ActionListener {
- Frame1 adaptee;
-
- Frame1_menuControlShowGlyphs_actionAdapter(Frame1 adaptee) {
- this.adaptee = adaptee;
- }
-
- public void actionPerformed(ActionEvent e) {
- adaptee.menuControlShowGlyphs_actionPerformed(e);
- }
- }
-
-
- /**
- * The default Application Wizard generated about box.
- *
- * @version 1.0, 06/10/97
- * @author Borland Development Team
- */
- class Frame1_AboutBox extends Dialog implements ActionListener {
- Panel panel1 = new Panel();
- BevelPanel bevelPanel1 = new BevelPanel();
- Frame1_InsetsPanel insetsPanel1 = new Frame1_InsetsPanel();
- Frame1_InsetsPanel insetsPanel2 = new Frame1_InsetsPanel();
- Frame1_InsetsPanel insetsPanel3 = new Frame1_InsetsPanel();
- Button button1 = new Button();
- ImageControl imageControl1 = new ImageControl();
- Label label1 = new Label();
- Label label2 = new Label();
- Label label3 = new Label();
- Label label4 = new Label();
- BorderLayout borderLayout1 = new BorderLayout();
- BorderLayout borderLayout2 = new BorderLayout();
- FlowLayout flowLayout1 = new FlowLayout();
- GridLayout gridLayout1 = new GridLayout();
- String product = "Simple TreeControl Sample";
- String version = "Version 1.0";
- String copyright = "Copyright (c) 1997";
- String comments = "";
-
- /**
- * Construct the frame and it's UI elements
- * @param parent The parent frame for the dialog
- */
- public Frame1_AboutBox(Frame parent) {
- super(parent);
- try {
- jbInit();
- }
- catch (Exception e) {
- borland.jbcl.util.Diagnostic.printStackTrace(e);
- }
- pack();
- }
-
- /**
- * Contains the UI elements and UI properties of the about box
- */
- void jbInit() throws Exception{
- this.setTitle("About");
- setResizable(false);
- this.add(panel1, null);
- panel1.setLayout(borderLayout1);
- bevelPanel1.setLayout(borderLayout2);
- insetsPanel2.setLayout(flowLayout1);
- insetsPanel2.add(imageControl1, null);
- insetsPanel2.setInsets(new Insets(10, 10, 10, 10));
- bevelPanel1.add(insetsPanel2, BorderLayout.WEST);
- gridLayout1.setRows(4);
- gridLayout1.setColumns(1);
- label1.setText(product);
- label2.setText(version);
- label3.setText(copyright);
- label4.setText(comments);
- insetsPanel3.setLayout(gridLayout1);
- insetsPanel3.add(label1, null);
- insetsPanel3.add(label2, null);
- insetsPanel3.add(label3, null);
- insetsPanel3.add(label4, null);
- insetsPanel3.setInsets(new Insets(10, 60, 10, 10));
- bevelPanel1.add(insetsPanel3, BorderLayout.CENTER);
- button1.setLabel("OK");
- button1.addActionListener(this);
- insetsPanel1.add(button1, null);
- panel1.add(insetsPanel1, BorderLayout.SOUTH);
- panel1.add(bevelPanel1, BorderLayout.NORTH);
- try {
- imageControl1.setImageName("sample.gif");
- }
- catch (Exception e) {
- borland.jbcl.util.Diagnostic.printStackTrace(e);
- }
- pack();
- }
-
- /**
- * Catch the action performed event on button1 to dismiss the
- * dialog.
- */
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == button1) {
- setVisible(false);
- dispose();
- }
- }
- }
-
- class Frame1_menuControlPluses_actionAdapter implements java.awt.event.ActionListener {
- Frame1 adaptee;
-
- Frame1_menuControlPluses_actionAdapter(Frame1 adaptee) {
- this.adaptee = adaptee;
- }
-
- public void actionPerformed(ActionEvent e) {
- adaptee.menuControlPluses_actionPerformed(e);
- }
- }
-
- class Frame1_menuConrolArrows_actionAdapter implements java.awt.event.ActionListener {
- Frame1 adaptee;
-
- Frame1_menuConrolArrows_actionAdapter(Frame1 adaptee) {
- this.adaptee = adaptee;
- }
-
- public void actionPerformed(ActionEvent e) {
- adaptee.menuConrolArrows_actionPerformed(e);
- }
- }
-
- class Frame1_menuControlTextEditor_actionAdapter implements java.awt.event.ActionListener{
- Frame1 adaptee;
-
- Frame1_menuControlTextEditor_actionAdapter(Frame1 adaptee) {
- this.adaptee = adaptee;
- }
-
- public void actionPerformed(ActionEvent e) {
- adaptee.menuControlTextEditor_actionPerformed(e);
- }
- }
-
- class Frame1_menuControlChoiceEditor_actionAdapter implements java.awt.event.ActionListener {
- Frame1 adaptee;
-
- Frame1_menuControlChoiceEditor_actionAdapter(Frame1 adaptee) {
- this.adaptee = adaptee;
- }
-
- public void actionPerformed(ActionEvent e) {
- adaptee.menuControlChoiceEditor_actionPerformed(e);
- }
- }
-
-
-